package ynu.edu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.common.exception.BadRequestException;
import ynu.edu.common.utils.BeanUtils;
import ynu.edu.order.domain.dto.LogisticsFormDTO;
import ynu.edu.order.domain.po.OrderLogistics;
import ynu.edu.order.domain.vo.OrderLogisticsVO;
import ynu.edu.order.mapper.OrderLogisticsMapper;
import ynu.edu.order.service.IOrderLogisticsService;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderLogisticsServiceImpl extends ServiceImpl<OrderLogisticsMapper, OrderLogistics> implements IOrderLogisticsService {

    @Override
    @Transactional
    public void createLogistics(Long orderId, LogisticsFormDTO logisticsFormDTO) {
        // 检查是否已存在物流信息
        OrderLogistics existingLogistics = getById(orderId);
        if (existingLogistics != null) {
            throw new BadRequestException("该订单已存在物流信息");
        }
        
        // 创建物流信息
        OrderLogistics logistics = BeanUtils.copyBean(logisticsFormDTO, OrderLogistics.class);
        logistics.setOrderId(orderId);
        logistics.setCreateTime(LocalDateTime.now());
        logistics.setUpdateTime(LocalDateTime.now());
        
        save(logistics);
        log.info("订单{}物流信息创建成功", orderId);
    }

    @Override
    @Transactional
    public void updateLogistics(Long orderId, LogisticsFormDTO logisticsFormDTO) {
        // 查询现有物流信息
        OrderLogistics logistics = getById(orderId);
        if (logistics == null) {
            throw new BadRequestException("该订单不存在物流信息");
        }
        
        // 更新物流信息
        BeanUtils.copyBean(logisticsFormDTO, logistics.getClass());
        logistics.setUpdateTime(LocalDateTime.now());
        
        updateById(logistics);
        log.info("订单{}物流信息更新成功", orderId);
    }

    @Override
    public OrderLogisticsVO queryLogistics(Long orderId) {
        OrderLogistics logistics = getById(orderId);
        if (logistics == null) {
            return null;
        }
        
        OrderLogisticsVO vo = BeanUtils.copyBean(logistics, OrderLogisticsVO.class);
        // 构建完整地址
        vo.setFullAddress(buildFullAddress(logistics));
        
        return vo;
    }

    @Override
    @Transactional
    public void deleteLogistics(Long orderId) {
        OrderLogistics logistics = getById(orderId);
        if (logistics == null) {
            throw new BadRequestException("该订单不存在物流信息");
        }
        
        removeById(orderId);
        log.info("订单{}物流信息删除成功", orderId);
    }

    /**
     * 构建完整地址
     */
    private String buildFullAddress(OrderLogistics logistics) {
        return String.format("%s%s%s%s", 
                logistics.getProvince(), 
                logistics.getCity(), 
                logistics.getTown(), 
                logistics.getStreet());
    }
}
